/*
 * Copyright (C) 2009-2018 Hangzhou 2Dfire Technology Co., Ltd. All rights reserved
 */
package com.ws.soa.consumer.payment.controller;

import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.ws.soa.consumer.payment.constant.Constants;
import com.ws.soa.consumer.payment.dto.WxPayFormDTO;
import com.ws.soa.consumer.service.SessionStorageClientService;
import com.ws.soa.consumer.session.UserSessionStorage;
import com.ws.soa.consumer.util.LogUtil;
import com.ws.soa.consumer.util.UUIDUtil;
import com.ws.soa.consumer.payment.vo.WxPayVO;

import org.slf4j.Logger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * WxPayController
 *
 * @author luobosi@2dfire.com
 * @since 2019-03-02
 */
@RestController
@RequestMapping("/pay")
public class WxPayController {

    @Resource
    private SessionStorageClientService sessionStorageClientService;

    @Resource
    private HashMap<String, WxPayServiceImpl> wxPayFacadeHashMap;

    private Logger loggerController = LogUtil.CONTROLLER;
    /**
     * 微信支付，统一下单、获取支付串
     * 客户端上传商品id、商品数量、总金额做简单校验
     * 调用consumer-center下单，获取orderId
     * 调用consumer-center保存支付信息
     * 调用微信接口获取签名支付串
     *
     * @param wxPayFormDTO 微信支付请求对象
     * @return WxPayVO
     * @throws Exception
     * @desc 微信支付
     */
    @PostMapping("/common/create_order")
    @ResponseBody
    public WxPayVO createOrder(HttpServletRequest request,
                               @Valid @RequestBody WxPayFormDTO wxPayFormDTO) throws Exception {
        // 拿到用户信息
        UserSessionStorage
            userSessionStorage = sessionStorageClientService.getUserSessionStorage(request);
        Long totalPrice = wxPayFormDTO.getTotalPrice();
        String outTradeNo = UUIDUtil.getUUID();

        //3、获取微信支付串
        WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest();
        wxPayUnifiedOrderRequest.setBody("波形计划");
        wxPayUnifiedOrderRequest.setAppid(userSessionStorage.getAppId());
        // 外部交易号
        wxPayUnifiedOrderRequest.setOutTradeNo(outTradeNo);
        wxPayUnifiedOrderRequest.setTotalFee((int)(totalPrice / 100));

        wxPayUnifiedOrderRequest.setSpbillCreateIp(request.getRemoteAddr());
        wxPayUnifiedOrderRequest.setTradeType(WxPayConstants.TradeType.APP);
        WxPayAppOrderResult
            wxPayAppOrderResult = wxPayFacadeHashMap.get(userSessionStorage.getAppId()).createOrder(wxPayUnifiedOrderRequest);
        WxPayVO wxPayVO = new WxPayVO();

        wxPayVO.setAppId(wxPayAppOrderResult.getAppId());
        wxPayVO.setPartnerid(wxPayAppOrderResult.getPartnerId());
        wxPayVO.setTimeStamp(wxPayAppOrderResult.getTimeStamp());
        wxPayVO.setNoncestr(wxPayAppOrderResult.getNonceStr());
        wxPayVO.setPackageStr(wxPayAppOrderResult.getPackageValue());
        wxPayVO.setSign(wxPayAppOrderResult.getSign());
        wxPayVO.setPrepayid(wxPayAppOrderResult.getPrepayId());


        return wxPayVO;
    }


}
